package com.hepinghospital.user.ui.activity.consult;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hepinghospital.user.R;
import com.hepinghospital.user.base.BaseActivity;
import com.hepinghospital.user.ui.activity.prescription.AtyHistoryCaseActivity;
import com.hepinghospital.user.ui.activity.prescription.MyMedicineActivity;
import com.hepinghospital.user.ui.dialog.EvaluateDialog;
import com.hepinghospital.user.util.ImageLoaderHelper;
import com.hepinghospital.user.util.ToastUtil;
import com.rest.business.RestProxy;
import com.rest.observer.BaseObserver;
import com.rest.response.DiagnoseOrderDetailResponse;

import butterknife.BindView;
import butterknife.OnClick;
import jz.joyoung.robot.util.UIConstants;

/**
 * @author DELL
 */
public class DiagnoseOrderDetailActivity extends BaseActivity {
    @BindView(R.id.img_head)
    ImageView imgHead;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_hospital)
    TextView tvHospital;
    @BindView(R.id.tv_recipel)
    TextView tvRecipel;
    @BindView(R.id.tv_consult_type)
    TextView tvConsultType;
    @BindView(R.id.tv_deptName)
    TextView tvDeptName;
    @BindView(R.id.tv_detail)
    TextView tvDetail;
    @BindView(R.id.tv_oneselfState)
    TextView tvOneselfState;
    @BindView(R.id.tv_main)
    TextView tvMain;
    @BindView(R.id.tv_advice)
    TextView tvAdvice;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.more)
    ImageView more;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.chat_container)
    RelativeLayout chatContainer;
    @BindView(R.id.rl_history_case)
    RelativeLayout rlHistoryCase;
    @BindView(R.id.evaluate_container)
    RelativeLayout evaluateContainer;
    @BindView(R.id.tv_evaluate)
    TextView tvEvaluate;
    @BindView(R.id.tv_star)
    TextView tvStar;

    String diagnoseId, orderId;
    int canEvaluate = 0, canChat;
    private EvaluateDialog mEvaluateDialog;

    @Override
    protected void initViews() {
        tvTitle.setText("详情");
    }

    @Override
    protected void initData() {
        diagnoseId = getIntent().getStringExtra("diagnoseId");
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.aty_diagnose_order_detail;
    }

    @Override
    protected void onResume() {
        super.onResume();
        diagnoseOrderDetail();
    }

    @OnClick({R.id.btn_back, R.id.chat_container, R.id.rl_my_prescription, R.id.rl_history_case, R.id.evaluate_container})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.chat_container:
                if (canChat == 1) {
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("diagnoseId", diagnoseId);
                    startActivity(ChatActivity.class, bundle);
                }
                break;
            case R.id.rl_my_prescription:
                startActivity(new Intent(mContext, MyMedicineActivity.class));
                break;
            case R.id.rl_history_case:
                startActivity(new Intent(mContext, AtyHistoryCaseActivity.class).putExtra("diagnoseId", diagnoseId));
                break;
            case R.id.evaluate_container:
                if (canEvaluate == 1) {
                    showEvaluateDialog();
                }
                break;
            default:
                break;
        }
    }

    /**
     * 评价弹框
     */
    private void showEvaluateDialog() {
        mEvaluateDialog = EvaluateDialog.newInstance(orderId);
        mEvaluateDialog.show(getSupportFragmentManager(), "EvaluateDialog");
        mEvaluateDialog.setmListener(new EvaluateDialog.OnItemClickListener() {
            @Override
            public void onConfirm(String score, String content) {
                if (TextUtils.isEmpty(score)) {
                    ToastUtil.showToast(mContext, "请给医生打分");
                    return;
                }
                if (TextUtils.isEmpty(content)) {
                    ToastUtil.showToast(mContext, "请输入评价内容");
                    return;
                }
                evaluateOrder(score, content);
            }

            @Override
            public void onCancel() {

            }
        });
    }

    /**
     * 订单评价
     *
     * @param score   评分（0.5到5分，间隔0.5分）
     * @param content 评价内容
     */
    private void evaluateOrder(String score, String content) {
        RestProxy.getInstance().evaluateOrder(orderId, score, content, new BaseObserver<String>() {
            @Override
            public void onSuccess(String result) {
                diagnoseOrderDetail();
                mEvaluateDialog.dismiss();
            }
        });
    }

    public void diagnoseOrderDetail() {
        RestProxy.getInstance().diagnoseOrderDetail(diagnoseId, new BaseObserver<DiagnoseOrderDetailResponse>() {

            @Override
            public void onSuccess(DiagnoseOrderDetailResponse diagnoseOrderDetailResponse) {
                ImageLoaderHelper.getInstance().GlideCircleImageLoader(DiagnoseOrderDetailActivity.this, diagnoseOrderDetailResponse.avatar, imgHead);
                tvName.setText(diagnoseOrderDetailResponse.docName);
                if (diagnoseOrderDetailResponse.isPrescription == 0) {
                    tvRecipel.setVisibility(View.GONE);
                } else {
                    tvRecipel.setVisibility(View.VISIBLE);
                }
                if (diagnoseOrderDetailResponse.businessType == UIConstants.GRAPHIC_TYPE_1) {
                    tvConsultType.setText(R.string.string_consult);
//                    tvConsultType.setBackgroundColor(ContextCompat.getColor(mContext, R.color.color_grey_button_normal));
                    tvRecipel.setVisibility(View.GONE);

                } else if (diagnoseOrderDetailResponse.businessType == UIConstants.GRAPHIC_TYPE_2) {
                    tvConsultType.setText(R.string.string_referral);
//                    tvConsultType.setBackgroundColor(ContextCompat.getColor(mContext, R.color.chooser_orange));
                    tvRecipel.setVisibility(View.VISIBLE);
                }

                tvDeptName.setText(diagnoseOrderDetailResponse.deptName);
                tvHospital.setText(diagnoseOrderDetailResponse.hosName);
                tvDetail.setText(diagnoseOrderDetailResponse.patientInfo);
                tvOneselfState.setText(diagnoseOrderDetailResponse.descriptionInfo);
                tvMain.setText(diagnoseOrderDetailResponse.mainDiagnosis);
                tvAdvice.setText(diagnoseOrderDetailResponse.docProposal);
                tvTime.setText(diagnoseOrderDetailResponse.endTime);
                if (!TextUtils.isEmpty(diagnoseOrderDetailResponse.score)) {
                    tvStar.setText(diagnoseOrderDetailResponse.score);
                    tvStar.setVisibility(View.VISIBLE);
                } else {
                    tvStar.setVisibility(View.GONE);
                }
                orderId = diagnoseOrderDetailResponse.orderId;
                canEvaluate = diagnoseOrderDetailResponse.canEvaluate;
                if (canEvaluate == 0) {
                    more.setVisibility(View.INVISIBLE);
                }
                if (TextUtils.isEmpty(diagnoseOrderDetailResponse.comment)) {
                    tvEvaluate.setVisibility(View.GONE);
                } else {
                    tvEvaluate.setVisibility(View.VISIBLE);
                    tvEvaluate.setText(diagnoseOrderDetailResponse.comment);
                }
                canChat = diagnoseOrderDetailResponse.canChat;
                ImageLoaderHelper.getInstance().GlideCircleImageLoader(DiagnoseOrderDetailActivity.this, diagnoseOrderDetailResponse.avatar, imgHead);
            }
        });
    }
}
